package com.example.h5shell.utils;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;

import java.util.ArrayList;
import java.util.HashMap;

public class ContactManagerHelper {

    private Context context;

    public ContactManagerHelper(Context context) {
        this.context = context;
    }

    /**
     * 读取所有联系人并返回它们的列表
     * @return 包含所有联系人信息的列表
     */

    public ArrayList<HashMap<String, String>> readContacts() {
        ArrayList<HashMap<String, String>> list = new ArrayList();

        Uri rawContactUri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri datatUri = Uri.parse("content://com.android.contacts/data");
        // 从raw_contacts中读取联系人的额id("contact_id")
        Cursor rawContacts = context.getContentResolver().query(rawContactUri, new String[] { "contact_id" }, null, null, null);
        if (rawContacts != null) {
            while (rawContacts.moveToNext()) {
                String contactId = rawContacts.getString(0);
                System.out.println("contactId==" + contactId);
                Cursor data = context.getContentResolver().query(datatUri, new String[] { "data1", "mimetype" }, "contact_id=?",
                        new String[] { contactId }, null);
                if (data != null) {
                    HashMap<String, String> map = new HashMap<String, String>();

                    while (data.moveToNext()) {
                        String data1 = data.getString(0);
                        String mimetype = data.getString(1);
                        if ("vnd.android.cursor.item/phone_v2".equals(mimetype)) {
                            map.put("phone", data1);
                        } else if ("vnd.android.cursor.item/name".equals(mimetype)) {
                            map.put("name", data1);
                        }

                    }
                    list.add(map);
                    data.close();
                }
            }
            rawContacts.close();
        }
        return list;
    }




    /**
     * 写入联系人到手机
     * @param name 联系人的名字
     * @param phoneNumber 联系人的电话号码
     * @return 写入是否成功
     */
    public boolean addContact(String name, String phoneNumber) {
        // 检查运行时权限（Android 6.0+）
        if (context.checkSelfPermission(android.Manifest.permission.WRITE_CONTACTS) != android.content.pm.PackageManager.PERMISSION_GRANTED) {
            // 这里应该提示用户授予权限，但为简化示例，直接返回false
            return false;
        }

        ContentResolver contentResolver = context.getContentResolver();

        // 插入RawContact
        Uri rawContactUri = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, new ContentValues());

        if (rawContactUri != null) {
            long rawContactId = ContentUris.parseId(rawContactUri);

            // 插入联系人名字
            ContentValues nameValues = new ContentValues();
            nameValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
            nameValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
            nameValues.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name);
            contentResolver.insert(ContactsContract.Data.CONTENT_URI, nameValues);

            // 插入电话号码
            ContentValues phoneValues = new ContentValues();
            phoneValues.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
            phoneValues.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
            phoneValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber);
            phoneValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
            contentResolver.insert(ContactsContract.Data.CONTENT_URI, phoneValues);

            return true;
        }

        return false;
    }


    /**
     * 联系人的类
     */
    public static class Contact {
        public String name;
        public String phoneNumber;

        public Contact(String name, String phoneNumber) {
            this.name = name;
            this.phoneNumber = phoneNumber;
        }


    }
}